package com.aizuda.boot.modules.business.feishu.strategy.impl;

import com.aizuda.boot.config.properties.FeiShuConfigProperties;
import com.aizuda.boot.modules.auth.domain.entity.ThirdUser;
import com.aizuda.boot.modules.system.entity.SysMessage;
import com.aizuda.boot.modules.business.feishu.domain.AtUsersDTO;
import com.aizuda.boot.modules.business.feishu.domain.BotValues;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Optional;

/**
 * @Author sangyirong
 * @ClassName WuZhiNotifyTemplateStrategy
 * @Version 1.0
 * @Date 2025/7/12 18:37
 */
@Data
@Component
@Schema(description = "悟智消息通知模版参数")
public class WuZhiNotifyTemplateStrategy extends AbstractTemplateParameterStrategy {

    private final FeiShuConfigProperties feiShuConfigProperties;

    // 目标租户 key
    private static final String TARGET_TENANT_KEY = "10b87b7f1897575f";


    /**
     * 获取业务范围配置
     */
    private List<String> getBusinessScope() {
        if (feiShuConfigProperties.getApps() == null) {
            return null;
        }

        Optional<FeiShuConfigProperties.FeiShuAppConfig> appConfig = feiShuConfigProperties.getApps()
                .values()
                .stream()
                .filter(config -> TARGET_TENANT_KEY.equals(config.getTenantKey()))
                .findFirst();

        if (appConfig.isEmpty() || appConfig.get().getTemplates() == null) {
            return null;
        }

        FeiShuConfigProperties.FeiShuTemplateConfig templateConfig = appConfig.get().getTemplates().get("notify-template");
        return templateConfig != null ? templateConfig.getBusinessScope() : null;
    }

    @Override
    public boolean supports(String tenantKey, String businessType) {
        if (null == tenantKey || null == businessType) {
            return false;
        }
        if (!TARGET_TENANT_KEY.equals(tenantKey)) {
            return false;
        }

        // 从配置中获取业务范围
        List<String> businessScope = getBusinessScope();
        return businessScope != null && businessScope.contains(businessType);
    }

    @Override
    public BotValues buildTemplateParameters(SysMessage message, ThirdUser thirdUser) {
        BotValues botValues = new BotValues();
        HashMap<String, String> map = new HashMap<>();
        map.put("url", "http://fimai.aidb.com.cn:3013/business/contract/all-contracts");
        botValues.setOpenId(thirdUser.getOpenId());
        botValues.setTenantKey(thirdUser.getTenantKey());
        botValues.setBusinessType(message.getBusinessType());
        botValues.addParam("title", message.getTitle())
                .addParam("content", message.getContent())
                .addParam("alarm_time", getCurrentTime())
                .addParam("type", getMessageTypeDescription(message.getBusinessType()))
                .addParam("href", map);
        return botValues;
    }

    @Override
    public BotValues buildAtTemplate(AtUsersDTO dto, ThirdUser thirdUser) {
        BotValues botValues = new BotValues();
        HashMap<String, String> map = new HashMap<>();
        map.put("url", dto.getUrl());
        botValues.setOpenId(thirdUser.getOpenId());
        botValues.setTenantKey(thirdUser.getTenantKey());
        botValues.setBusinessType(dto.getBusinessType());
        botValues.addParam("title", dto.getTitle())
                .addParam("content", dto.getContent())
                .addParam("alarm_time", getCurrentTime())
                .addParam("type", getMessageTypeDescription(dto.getBusinessType()))
                .addParam("href", map);
        return botValues;
    }
}
